<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-14 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{		
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}

	private function getList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		$this->request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$this->request['is_gov'] = '1';
		
		$this->request['from'] = $this->request['to'] = '';
		
		$result = array();
		switch($type)
		{
			case 'seafood':
				$this->request['from'] = $this->request['to'] = date('Y-m-d', get_new_date_seafood_history_price());
				$result = get_seafood_history_price($this->request);
				break;
			case 'hostel':
				//$this->request['from'] = $this->request['to'] = date('Y-m-d', get_new_date_hostel_history_price());
				$result = get_hostel_history_price($this->request);
				break;
			case 'scenic':
				$this->request['from'] = $this->request['to'] = date('Y-m-d', get_new_date_scenic_history_price());
				$result = get_scenic_history_price($this->request);
				break;
			default:
				$result['result'] = array();
				break;	
		}
		
		if(!check_array($result['result'])) $this->message('gp_01', STATUS_F);
		
		$list = array();
		
		if($type == 'hostel')
		{
			$item_id = '';
			$room_list = array();
			$room_temp = array();
			
			foreach($result['result'] as $temp)
			{
				$item_id = $temp['item_id'];
				
				unset($temp['count(distinct ti.`id`)']);
				unset($temp['room_name']);
				unset($temp['price']);
				unset($temp['range']);
				
				$temp['create_date'] = date('Y-m-d', $temp['create_date']);
				
				$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
				$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
				
				if(isset($this->request['latitude']) && isset($this->request['longitude']))
				{
					$distance = get_distance($this->request['latitude'], $this->request['longitude'], $temp['latitude'], $temp['longitude']);
					$temp['distance'] = $distance;
				}
				
				$result = get_hostel_room_history_price(array('id'=>$item_id, 
														'from'=>$this->request['from'], 
														'to'=>$this->request['to'], 
														'limit'=>'500',
														'is_gov'=>'1'));
				$room_list = $result['result'];
				
				if(check_array($room_list))
				{
					foreach($room_list as $temp2)
					{
						$room_temp['id'] = $temp2['id'];
						$room_temp['name'] = $temp2['room_name'];
						$room_temp['price'] = $temp2['price'];
					
						$temp['room_list'][] = $room_temp;
					}
				}
								
				$list[] = $temp;
			}
			
			if(isset($this->request['latitude']) && isset($this->request['longitude']))
			{
				usort($list, 'sort_by_dist_asc');
			}
		}
		else
		{
			foreach($result['result'] as $temp)
			{
				$temp['create_date'] = date('Y-m-d', $temp['create_date']);
				
				$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
				$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
				
				$list[] = $temp;
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = array_values($list);
	}
}